package com.tanhua.houtai.service;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.houtai.mapper.LogsMapper;
import com.tanhua.houtai.pojo.Logs;
import com.tanhua.houtai.vo.PageResult1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogsService {
    @Autowired
    private LogsMapper logsMapper;

    /**
     *
     * @param page
     * @param pagesize
     * @param name
     * @param content
     * @param startTime
     * @param endTime
     * @return
     */
    public PageResult1 getLogs(Integer page, Integer pagesize, String name, String content, String startTime, String endTime){
        PageResult1 pageResult1 = new PageResult1();
        pageResult1.setPage(page);
        pageResult1.setPagesize(pagesize);

        QueryWrapper<Logs> queryWrapper = new QueryWrapper<>();

        if (!(name==null)){
            queryWrapper.eq("operator",name);
        }
        if (!(content==null)){
            queryWrapper.eq("content",content);
        }
        if (!(startTime==null&&endTime==null)){
            queryWrapper.between("time",startTime,endTime);
        }

        queryWrapper.orderByAsc("logId");
        List<Logs> logsList = this.logsMapper.selectList(queryWrapper);

        pageResult1.setItems(logsList);

        return pageResult1;
    }

    public void saveLog(String operator,String ip, String content, String log){
        QueryWrapper queryWrapper = new QueryWrapper();

        // 创建保存日志的对象
        Logs logs = new Logs();
        // 往对象中添加数据
        logs.setOperator(operator);
        logs.setIp(ip);

        // 对接收的时间数据进行转换
        String time= DateUtil.today();

        logs.setTime(time);
        logs.setContent(content);
        logs.setLog(log);

        this.logsMapper.insert(logs);
    }


    public Logs queryLogpage(Integer id){

        // 根据查询条件查询单条数据
        Logs logs = this.logsMapper.selectById(id);
        return  logs;
    }

}
